package sandbox.j2se;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class RegexTest {

    private static final String[] texts = new String[] {
        "INSERT INTO fias_address_object (formal_name, live_status, region_code, guid, short_name, off_name, act_status, parent_guid, level, id) VALUES('Березка--6', 0, '77', '28357ec2-5163-4008-9bb4-191127ce3dc4', 'снт', 'СНТ Березка--6', 0, '763b0e3a-4876-4e5a-858a-3cf48493cf74', 90, 'ae14745b-a41b-4302-8bec-e0540d5d474b');",
        "INSERT INTO fias_address_object (formal_name, live_status, region_code, guid, short_name, off_name, act_status, parent_guid, level, id) VALUES('Бере'зка--6', 0, '77', '28357ec2-5163-4008-9bb4-191127ce3dc4', 'снт', 'СНТ Березка--6', 0, '763b0e3a-4876-4e5a-858a-3cf48493cf74', 90, 'ae14745b-a41b-4302-8bec-e0540d5d474b');",
        "--INSERT INTO fias_address_object (formal_name, live_status, region_code, guid, short_name, off_name, act_status, parent_guid, level, id) VALUES('Березка--6', 0, '77', '28357ec2-5163-4008-9bb4-191127ce3dc4', 'снт', 'СНТ Березка--6', 0, '763b0e3a-4876-4e5a-858a-3cf48493cf74', 90, 'ae14745b-a41b-4302-8bec-e0540d5d474b');"
    };

    public static void main(String[] args) {
        for(String text: texts) {
            System.out.println("-------------------------------------------");
            System.out.println("*  text: " + text);
            for(String regex: args) {
                testRegex(regex, text);
            }
        }
    }

    public static void test() {
        String source = "abc ';'; foobar;";
        String[] parts = source.split(";(?!\\s*('|\"))");
        for (String part : parts) {
            System.out.println(part);
        }
        System.out.println(" a   ".matches("^\\s*$"));

        source = "drop create -v";
        parts = source.split("\\s+(?=\\-)");
        for (String part : parts) {
            System.out.println(part);
        }
    }

    private static void testRegex(String regex, String text) {
        int begin = 0;
        int end = 0;
        Pattern p = Pattern.compile(regex);
        Matcher m = p.matcher(text);
        System.out.println("* regex: " + regex);
        System.out.println();
        while(m.find()) {
            begin = m.start();
            end = m.end();
            System.out.println(text.substring(begin, end));
            begin = end;
        }
    }
}
